package com.datn.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.datn.DataAccess.UserDatabaseAdapter;
import com.datn.beans.UserBean;
import com.datn.utils.AlertCustom;
import com.datn.utils.FileUtils;
import com.datn.utils.GPSUtils;
import com.datn.utils.HelloItemizedOverlay;
import com.datn.utils.PreferenceUtils;
import com.datn.utils.places.JsonPlace;
import com.datn.utils.places.Place;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class LoginActivity extends MapActivity implements OnClickListener   {
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        PreferenceUtils.getInstance(this);
      //Test Database
        Button btLogin=(Button)findViewById(R.id.btLogin);
        Button btCancel=(Button)findViewById(R.id.btBackLogin);
        
        btLogin.setOnClickListener(this);
        btCancel.setOnClickListener(this);
        
        TextView tvUsername=(TextView)findViewById(R.id.tfUsernameLogin);
        TextView tvPassword=(TextView)findViewById(R.id.tfPasswordLogin);
        
        //Init Preference
        PreferenceUtils.setUser("admin", "admin", "admin");
        
        tvUsername.setText(PreferenceUtils.getPreference(PreferenceUtils.KEY_USERNAME));
        tvPassword.setText(PreferenceUtils.getPreference(PreferenceUtils.KEY_PASSWORD));
       
    }

	

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.btLogin:	
			processClickLoginButton();		
			break;
		case R.id.btBackLogin:
			processBackButton();
			break;
		default:
			break;
		}
	}
	
	public void processBackButton(){
		Intent intentBack = new Intent(this, NearLocationActivity.class);
		startActivity(intentBack);
		finish();
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if (keyCode==KeyEvent.KEYCODE_BACK){
			processBackButton();
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	
	public void processClickLoginButton() {
		
		EditText edtUsername=(EditText)findViewById(R.id.tfUsernameLogin);
		EditText edtPassword=(EditText)findViewById(R.id.tfPasswordLogin);
		
		UserDatabaseAdapter adapter=new UserDatabaseAdapter(this);
		adapter.open();
		if (adapter.isUser(edtUsername.getText().toString().trim(),edtPassword.getText().toString().trim())){
					UserBean.username=edtUsername.getText().toString().trim();
					UserBean.password=edtPassword.getText().toString().trim();
					UserBean.role=0;
					UserBean.isLogin=true;
					
					Intent intent=new Intent(this, NearLocationActivity.class);	
					Toast.makeText(this, "Login success", Toast.LENGTH_LONG).show();
					startActivity(intent);
					finish();
		}
		else{
			UserBean.username="";
			UserBean.password="";
			UserBean.role=0;
			UserBean.isLogin=false;
			/*AlertCustom.showAlert(this, "Error", "Login Failed\nPlease check your username and password");*/
			Toast.makeText(this, "Login Failed\nPlease check your username and password", Toast.LENGTH_LONG).show();
		}
		adapter.close();
	}


}